;
(function (window, angular) {
    'use strict';
    angular.module("Controller.Report.PriceSearch", [])
        .controller("PriceSearchCtrl", ["$scope", "ybWidget", "ybHttp", "MESG_TYPE", "$timeout", "$filter", function ($scope, ybWidget, ybHttp, MESG_TYPE, $timeout, $filter) {
            $scope.markInfo = {
                curRow: -1
            };
            $scope.pageInfo = {
                totalPages: 1,
                totalRows: 0,
                pageSize: 10,
                curPage: 1
            };

            $scope.tbConf = {
                scrollWidth: 1250,
                tableCol: [
                    {
                        type: "",
                        name: "工厂名称",
                        dataField: "companyName",
                        width: 400,
                        isShow: true,
                        isFixed: false,
                    },
                    {
                        type: "",
                        name: "物料编码",
                        dataField: "materialsNo",
                        width: 200,
                        isShow: true,
                        isFixed: false,
                    },
                    {
                        type: "",
                        name: "物料描述",
                        dataField: "materialsDesc",
                        width: 250,
                        isShow: true,
                        isFixed: false,
                    },
                    {
                        type: "",
                        name: "价格组",
                        dataField: "priceGroup",
                        width: 200,
                        isShow: true,
                        isFixed: false,
                    },
                    {
                        type: "",
                        name: "价格",
                        dataField: "priceShow",
                        width: 200,
                        isShow: true,
                        isFixed: false,
                    }
                ]
            };

            $scope.condition = {};
            $scope.toolbarConfig = {};
            $scope.dataList = []; 
            $scope.componyList = [];
            $scope.priceGroup = [];

            $scope.switchToolbar = function (toolbar) {
                if (!!$scope.toolbarConfig["show" + toolbar]) {
                    $scope.toolbarConfig["show" + toolbar] = false;
                } else {
                    $timeout(function () {
                        $scope.toolbarConfig["show" + toolbar] = true;
                    });
                }
            };

            $scope.reset = function () {
                $scope.condition = {};
            };

            $scope.selectRow = function (index) {
                $scope.markInfo.curRow = index;
            };

            function getDataList (curPage) {
                var service = "device";
                var reqUrl = "/marketingActivity/priceSearchReportList";
                var param = {};
                if (!!curPage) {
                    $scope.pageInfo.curPage = curPage;
                }
                param.pageSize = $scope.pageInfo.pageSize;
                param.pageNum = $scope.pageInfo.curPage;

                param.filters = angular.copy($scope.condition);
                delete param.filters.materialsNoT;

                ybHttp.Post(service, reqUrl, param).then(function (resp) {
                    if (resp.code !== 0) {
                        ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                        return;
                    }
                    var resT = resp.value || {};
                    var pageInfoT = resT.pageInfo || {};

                    angular.forEach(resT.pageInfoList,function(item) {
                        item.priceShow = $filter("decimalPlaces")(item.price/100 || 0, 2);
                    })
                    $scope.dataList = resT.pageInfoList;
                    $scope.pageInfo.totalRows = pageInfoT.total;
                    $scope.pageInfo.totalPages = pageInfoT.pages;
                });
            }
            
            function getCompanyList () {
                var service = "device";
                var reqUrl = "/company/listCoreComs"; 
                ybHttp.Post(service, reqUrl,{}).then(function (resp) {
                    if (resp.code !== 0) {
                        ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                        return;
                    }
                    $scope.companyList = resp.value;
                });
            }
            $scope.plantChange = function (val) {
                $scope.condition.priceGroup = undefined;
                var service = "device";
                var reqUrl = "/marketingActivity/listPriceGroup"; 
                ybHttp.Get(service, reqUrl,{ orgId: val }).then(function (resp) {
                    if (resp.code !== 0) {
                        ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                        return;
                    }
                    var resT = resp.value || [];
                    $scope.priceGroup = resT.filter(function(item){return !!item.priceGroup});
                });
            };

            $scope.search = function (curPage) {
                getDataList(curPage);
            };

            // 处理中文逗号、换行符、空白字符
            $scope.handleBlur = function(e) {
                var val = e.target.value;
                var multiCodesT = val.replace(/[\n|\s|,|\uff0c]+/g, ',');
                if (multiCodesT && multiCodesT[0] === ',') {
                    multiCodesT = multiCodesT.substr(1);
                }
                if (multiCodesT && multiCodesT[multiCodesT.length - 1] === ',') {
                    multiCodesT = multiCodesT.slice(0, -1);
                }
                $scope.condition.materialsNos = multiCodesT;
            };

            (function () {
                getDataList();
                getCompanyList();
            })();
        }])
        ;
})(window, window.angular);